﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExORMDesginer.Models;
using MsdevManager;
using System.Collections.ObjectModel;
using EnvDTE;
using System.Threading.Tasks;

namespace ExORMDesginer.Services
{
    /// <summary>
    /// 有关运行中的VS编辑器相关服务
    /// </summary>
    public class VSIDEService
    {
        /// <summary>
        /// 已连接的服务,空表示服务已关闭。
        /// </summary>
        public static IDEInfo ConnectionVSIde { set; get; }

        /// <summary>
        /// 关闭服务
        /// </summary>
        public static void Close()
        {
            ConnectionVSIde.IDE = null;
            ConnectionVSIde=null;
        }

        /// <summary>
        /// 得到所有运行的VS编辑器
        /// </summary>
        /// <returns></returns>
        public static List<IDEInfo> GetRunIde()
        {
            List<IDEInfo> ides = new List<IDEInfo>();
            var hashTable = Msdev.GetIDEInstances(true);
            foreach(var key in hashTable.Keys)
            {
                var item = hashTable[key] as EnvDTE.DTE;

                IDEInfo info = new IDEInfo();
                info.Name = System.IO.Path.GetFileName(item.Solution.FullName);
                info.SolutionPath = item.Solution.FullName;
                info.IDE = item;
                ides.Add(info);
            }
            return ides;
        }

        /// <summary>
        /// 打开连接着的VS编辑器
        /// </summary>
        public static void ShowIDE()
        {
            if (ConnectionVSIde != null)
                Msdev.ShowIDE(ConnectionVSIde.IDE);
        }

        /// <summary>
        /// 获取解决方案路径信息和文件项
        /// </summary>
        /// <returns></returns>
        public static async Task<ObservableCollection<ProjectItemInfo>> GetPathInfo()
        {
            ObservableCollection<ProjectItemInfo> itemInfoList = null;
            if (ConnectionVSIde != null)
            {
                itemInfoList = new ObservableCollection<ProjectItemInfo>();
                var solution = ConnectionVSIde.IDE.Solution;

                foreach (Project proItem in solution.Projects)
                {
                    var root = new ProjectItemInfo();
                    root.Name = proItem.Name;
                    root.FullPath = proItem.FullName;
                    root.Tag = proItem.ProjectItems;
                    root.ItemType = ProjectItemType.Project;

                    itemInfoList.Add(root);

                    foreach (ProjectItem pi in proItem.ProjectItems)
                        getSubProjectItem(root,pi);
                }
            }
            return itemInfoList;
        }

        /// <summary>
        /// 得到子项目信息
        /// </summary>
        /// <param name="parent">父节点</param>
        /// <param name="proItem">文件项</param>
        private static async Task getSubProjectItem(ProjectItemInfo parent,ProjectItem proItem)
        {

            var fullPath = proItem.Properties.Item("FullPath").Value.ToString();
            if (System.IO.Directory.Exists(fullPath) && proItem.Name.ToLower() != "properties")
            {
                var directory = new ProjectItemInfo();
                directory.Name = proItem.Name;
                directory.FullPath = fullPath;
                directory.Tag = proItem.ProjectItems;
                directory.ItemType = ProjectItemType.Directory;

                parent.ProjectItemInfoList.Add(directory);

                foreach (ProjectItem pi in proItem.ProjectItems)
                    getSubProjectItem(directory, pi);
            }
        }


    }
}
